/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.builder;

import java.util.List;
import javax.swing.JButton;
import org.balisunrise.application.Application;
import org.balisunrise.builder.BuilderException;
import org.balisunrise.common.TransactionActions;
import org.balisunrise.swing.transaction.panel.TransactionActionPanel;
import org.balisunrise.swing.transaction.panel.layout.TransactionActionLayoutBuilder;

/**
 *
 * @author Glauco
 */
public class TransactionActionBuilder implements
        org.balisunrise.builder.TransactionActionBuilder{

    private static final String MESSAGE = "Erro ao construir um TransactionAction.\n";

    
    public TransactionActions create(Class entityClass, String alias)
            throws BuilderException {

        if(entityClass == null){
            throw new BuilderException(MESSAGE +
                    "entityClass não pode ser nulo.");
        }

        if(alias == null){
            throw new BuilderException(MESSAGE +
                    "alias não pode ser nulo.");
        }

        org.balisunrise.swing.transaction.TransactionActions ta =
                new org.balisunrise.swing.transaction.TransactionActions();

        ta.setAlias(alias);
        ta.setEntityClass(entityClass);

        // cria as ações
        try{
            List<org.balisunrise.common.VisualAction> list = Application.get()
                    .getVisualEntityManager()
                    .getVisualActionBuilder()
                    .createTransactionAction(ta);

            // cria representação visual
            ta.setVisualRepresentation(new TransactionActionPanel(
                    (JButton) list.get(0).getVisualRepresentation(),
                    (JButton) list.get(1).getVisualRepresentation(),
                    (JButton) list.get(2).getVisualRepresentation(),
                    (JButton) list.get(3).getVisualRepresentation(),
                    null,
                    new TransactionActionLayoutBuilder()));

            // nome do painel
            String name = entityClass.getName() + "_transactionAction_" + alias;
            ta.getVisualRepresentation().setName(name);
            ta.getVisualRepresentation().getAccessibleContext().setAccessibleName(name);
            
        } catch (BuilderException ex){
            System.out.println(MESSAGE + ex.getMessage());
        }

        return ta;
    }

}
